package tests;

import common.Utils;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 10715
 * Date: 2024-08-23
 * Time: 22:00
 */
public class LoginPage extends Utils {
    public static String url = "http://192.168.47.135:8653/blog_system/blog_login.html";
    Class<?> targetclass = LoginPage.class;
    public LoginPage() {
        super(url);
    }

    /*
    检查页面是否加载成功
     */
    public void loginPage() throws NoSuchMethodException, IOException {
        Method method = targetclass.getMethod("loginPage");
        String methodName = method.getName();
        driver.findElement(By.cssSelector("body > div.container-login > div"));
        getScreenShot(LoginPage.class.getName() + "-" + methodName);
        driver.navigate().refresh();
    }

    /*是否可以正常登陆*/
    public void loginSuccess() {
        driver.navigate().refresh();
        String methodName = "";
        try {
            methodName = targetclass.getMethod("loginSuccess").getName();
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        }
        driver.findElement(By.cssSelector("#username")).sendKeys("zhangsan");
        driver.findElement(By.cssSelector("#password")).sendKeys("123456");
        driver.findElement(By.cssSelector("#submit")).click();
        try {
            getScreenShot(LoginPage.class.getName() + "-" + methodName);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        //判断是否登陆成功
        driver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)"));
        try {
            getScreenShot(LoginPage.class.getName() + '-' + methodName);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public void loginFailByErrorUserName() throws InterruptedException {
        driver.navigate().back();
        String methodName = "";
        try {
            methodName = targetclass.getMethod("loginFailByErrorUserName").getName();
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        }
        driver.navigate().refresh();
        driver.findElement(By.cssSelector("#username")).sendKeys("zhangsan122");
        driver.findElement(By.cssSelector("#password")).sendKeys("1234567");
        try {
            getScreenShot(LoginPage.class.getName() + "-" + methodName);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        driver.findElement(By.cssSelector("#submit")).click();
        //出现弹窗
        Thread.sleep(1000);
        Alert alert = driver.switchTo().alert();
        String val = alert.getText();
        alert.accept();
        //判断是否为登陆页面
        driver.findElement(By.cssSelector("body > div.container-login > div"));
        try {
            getScreenShot(LoginPage.class.getName() + "-" + methodName);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        System.out.println("loginFailByErrorUserName---"+val);
        driver.navigate().refresh();
    }

    public void loginFailByNull() throws InterruptedException {
        driver.navigate().refresh();
        String methodName = "";
        try {
            methodName = targetclass.getMethod("loginFailByNull").getName();
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        }
        driver.navigate().refresh();
        try {
            getScreenShot(LoginPage.class.getName() + "-" + methodName);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        driver.findElement(By.cssSelector("#submit")).click();
        //出现弹窗
        Thread.sleep(1000);
        Alert alert = driver.switchTo().alert();
        String val = alert.getText();
        alert.accept();
        //判断是否为登陆页面
        driver.findElement(By.cssSelector("body > div.container-login > div"));
        try {
            getScreenShot(LoginPage.class.getName() + "-" + methodName);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        System.out.println("loginFailByNull---" + val);
        driver.navigate().refresh();
    }
}